﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using SaleManagement.Web.App_Code;
using SaleManagement.Services;
using SaleManagement.Domains;

namespace SaleManagement.Web.Pages
{
  public partial class PurchaseList : BasePage
  {
    #region Variables and properties

    private const string SEARCH_CUSTOMER_CODE = "PurchaseList_Search_Customer_Code";
    private const string SEARCH_CUSTOMER_NAME = "PurchaseList_Search_Customer_Name";
    private const string SEARCH_CUSTOMER_PHONE = "PurchaseList_Search_Phone";
    private const string SEARCH_FROMDATE = "PurchaseList_Search_FromDate";
    private const string SEARCH_TODATE = "PurchaseList_Search_ToDate";
    private const string SEARCH_INVOICENO = "PurchaseList_Search_InvoiceNo";

    private string _innerError = string.Empty;
    #endregion

    #region Events
    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsPostBack && !IsCallback)
      {
        deFromDate.Date = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
        deToDate.Date = DateTime.Now;

        #region Load Session

        txtCustomerCode.Text = GetSessionSearch(SEARCH_CUSTOMER_CODE);
        txtCustomerName.Text = GetSessionSearch(SEARCH_CUSTOMER_NAME);
        txtCustomerPhone.Text = GetSessionSearch(SEARCH_CUSTOMER_PHONE);
        txtPurchaseCode.Text = GetSessionSearch(SEARCH_INVOICENO);
        if (GetSessionSearch(SEARCH_FROMDATE) != null)
        {
          if (GetSessionSearch(SEARCH_FROMDATE) != string.Empty)
          {
            deFromDate.Date = Convert.ToDateTime(GetSessionSearch(SEARCH_FROMDATE));
          }
          else
          {
            deFromDate.Value = null;
          }
        }

        if (GetSessionSearch(SEARCH_TODATE) != null)//not in session
        {
          if (GetSessionSearch(SEARCH_TODATE) != string.Empty)//empty value
          {
            deToDate.Date = Convert.ToDateTime(GetSessionSearch(SEARCH_TODATE));
          }
          else
          {
            deToDate.Value = null;
          }
        }

        #endregion

        LoadPurchase();
      }
    }

    protected void gvPurchase_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
    {
      string[] para = e.Parameters.Split('|');
      if (para[0] == "Delete")
      {
        _innerError = DeletePurchase(Convert.ToInt32(para[1]));
      }

      LoadPurchase();
    }

    protected void gvPurchase_CustomJSProperties(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewClientJSPropertiesEventArgs e)
    {
      e.Properties["cpInnerError"] = _innerError;
    }
    #endregion

    #region Methods
    private void LoadPurchase()
    {
      string customerCode = Utilities.SafeString(txtCustomerCode.Text);
      string customerName = Utilities.SafeString(txtCustomerName.Text);
      string customerPhoneNumber = Utilities.SafeString(txtCustomerPhone.Text);
      string purchaseCode = Utilities.SafeString(txtPurchaseCode.Text);
      DateTime? fromDate = deFromDate.Value as DateTime?;
      DateTime? toDate = deToDate.Value as DateTime?;

      ConfigSessionSearch(SEARCH_CUSTOMER_CODE, customerCode);
      ConfigSessionSearch(SEARCH_CUSTOMER_NAME, customerName);
      ConfigSessionSearch(SEARCH_CUSTOMER_PHONE, customerPhoneNumber);
      ConfigSessionSearch(SEARCH_FROMDATE, fromDate.HasValue ? fromDate.Value.ToString() : string.Empty);
      ConfigSessionSearch(SEARCH_TODATE, toDate.HasValue ? toDate.Value.ToString() : string.Empty);
      ConfigSessionSearch(SEARCH_INVOICENO, purchaseCode);

      gvPurchase.DataSource = new PurchaseService().Search(customerCode, customerName, customerPhoneNumber, purchaseCode, fromDate, toDate);
      gvPurchase.DataBind();
    }

    private string DeletePurchase(int id)
    {
      #region Check detail
      TList<PurchaseDetail> detailList = new PurchaseDetailService().GetByPurchaseId(id);
      if (detailList.Count > 0)
      {
        return "Không thể xóa hóa đơn vì các thông tin chi tiết vẫn còn lưu.";
      }

      TList<Domains.PaymentDetail> paymentList = new PaymentDetailService().GetByPurchaseId(id);
      if (paymentList.Count > 0)
      {
        return "Không thể xóa hóa đơn vì hóa đơn đã được trả tiền.";
      }
      #endregion

      #region Delete DB
      try
      {
        new PurchaseService().Delete(id);
        return string.Empty;
      }
      catch (Exception ex)
      {
        WriteLog("PurchaseList::DeletePurchase", ex);
        return "Có lỗi trong quá trình xóa dữ liệu";
      }
      #endregion
    }
    #endregion
  }
}
